package com.heima.search.listen;

import com.heima.feigns.ArticleFeign;
import com.heima.model.common.constants.message.NewsUpOrDownConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.exception.CustException;
import com.heima.model.search.vo.SearchArticleVO;
import com.heima.search.service.ArticleSearchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.webresources.ClasspathURLStreamHandler;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ArticleAddOrRemoveListener {
    @Autowired
    ArticleFeign articleFeign;
    @Autowired
    ArticleSearchService articleSearchService;

    @RabbitListener(queuesToDeclare = @Queue(value = NewsUpOrDownConstants.NEWS_UP_FOR_ES_QUEUE))
    public void listenNewsUpMsg(String articleId) {
        log.info("接收到文章上架消息 , 消息内容 : {}",articleId);
        // 根据articleId查询文章信息,将文章信息 添加到es索引库中
        // 远程查询article信息
        ResponseResult<SearchArticleVO> result = articleFeign.findArticle(Long.valueOf(articleId));
        if(!result.checkCode()){
            CustException.cust(AppHttpCodeEnum.REMOTE_SERVER_ERROR,"远程调用查询文章信息失败");
        }
        SearchArticleVO articleVO = result.getData();
        articleSearchService.saveArticle(articleVO);
    }
    @RabbitListener(queuesToDeclare = @Queue(value = NewsUpOrDownConstants.NEWS_DOWN_FOR_ES_QUEUE))
    public void listenNewsDownMsg(String articleId) {
        log.info("接收到文章下架消息 , 消息内容 : {}",articleId);
        // 根据articleId查询文章信息,将文章信息从es索引库中删除
        articleSearchService.deleteArticle(articleId);

    }
}